import Vue from 'vue';
import VueRouter from 'vue-router';
import { setCurrentPageId } from '../utils/trackHandler';
import pageA from './pageA';
Vue.use(VueRouter);
export default pathPrefix => {
  let routes = [
    ...pageA,
    {
      path: '/*',
      name: '404',
      component: resolve =>
        require.ensure([], () =>
          resolve(require('src/pages/errorPage'), 'errorPage')
        )
    }
  ];

  routes = routes.map(route => {
    return {
      ...route,
      path: pathPrefix + route.path
    };
  });

  const oRouter = new VueRouter({
    mode: 'hash',
    scrollBehavior(to, from, savedPosition) {
      const microAppContent = document.querySelector(
        `div[data-name=${PACKAGE_NAME}]`
      );
      if (microAppContent) {
        const selector =
          microAppContent && microAppContent.parentNode
            ? microAppContent.parentNode
            : microAppContent.parentElement
            ? microAppContent.parentElement
            : '';
        if (selector) {
          selector.scrollTop = 0;
        }
      }
      return {
        x: 0,
        y: 0
      };
    },
    routes
  });

  oRouter.afterEach(to => {
    setCurrentPageId(to);
  });

  return oRouter;
};
